home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Storage / Bento / PrmRslvr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  9.5 KB  |  343 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PrmRslvr.cpp
  3.  
  4.     Contains:    Implementation of Storage Unit for the Mac Bento Container Suite.
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     9/27/96    CC        1382481: ResolvePromise erroneously calling
  13.                                     Drag&Drop to fulfill part promise.
  14.          <2>     9/26/96    CC        1391398 - Release & Acquire missing when
  15.                                     replacing an existing promise.
  16.         <12>     8/18/95    VL        1267187: Refcount sourcepart correctly.
  17.         <11>     6/29/95    VL        1242642: delete suView in CATCH block of
  18.                                     ResolvePromise.
  19.         <10>     6/16/95    VL        1259613: Fixed up ResolvePromise to handle
  20.                                     SetOffset on an unfocused storage unit.
  21.          <9>     5/26/95    VL        1251403: Multithreading naming support.
  22.          <8>     4/10/95    VL        1236490: Made GetPromiseValue work during
  23.                                     FulfillPromise.
  24.          <7>     3/14/95    VL        1225208,1225201: Added IsSettingPromise to
  25.                                     ensure that we don't call ResolvePromise
  26.                                     while setting a promise.
  27.          <6>    11/14/94    VL        1188257: Use Bento errors in BenotDef.h.
  28.          <5>     10/4/94    VL        1153379: Do not include SUPriv.h anymore.
  29.          <4>     9/29/94    RA        1189812: Mods for 68K build.
  30.          <3>     9/15/94    VL        1184871: Fixed ResolvePromise to handle
  31.                                     Drag and Drop correctly.
  32.          <2>     8/11/94    VL        1180299: Use modified CreateView.
  33.          <1>     7/26/94    VL        first checked in
  34.  
  35.     To Do:
  36. */
  37.  
  38. #ifndef _PRMRSLVR_
  39. #include "PrmRslvr.h"
  40. #endif
  41.  
  42. #ifndef SOM_CMStorageUnit_xh
  43. #include <CMSU.xh>
  44. #endif
  45.  
  46. #ifndef _ODMEMORY_
  47. #include <ODMemory.h>
  48. #endif
  49.  
  50. #ifndef __CM_API__
  51. #include <CMAPI.h>
  52. #endif
  53.  
  54. #ifndef _EXCEPT_
  55. #include <Except.h>
  56. #endif
  57.  
  58. #ifndef SOM_ODStorageUnitView_xh
  59. #include <SUView.xh>
  60. #endif
  61.  
  62. #ifndef SOM_ODStorageUnitCursor_xh
  63. #include <SUCursor.xh>
  64. #endif
  65.  
  66. #ifndef SOM_ODPart_xh
  67. #include <Part.xh>
  68. #endif
  69.  
  70. #ifndef SOM_ODSession_xh
  71. #include <ODSessn.xh>
  72. #endif
  73.  
  74. #ifndef SOM_ODDragAndDrop_xh
  75. #include <DragDrp.xh>
  76. #endif
  77.  
  78. #ifndef _BENTODEF_
  79. #include "BentoDef.h"
  80. #endif
  81.  
  82. #ifndef _ODDEBUG_
  83. #include "ODDebug.h"    // Adkins -- added
  84. #endif
  85.  
  86. //==============================================================================
  87. // PromiseResolver
  88. //==============================================================================
  89.  
  90. //------------------------------------------------------------------------------
  91. // PromiseResolver::IsPromiseValue
  92. //------------------------------------------------------------------------------
  93.  
  94. ODBoolean PromiseResolver::IsPromiseValue(Environment* ev)
  95. {        
  96.     CMValue value = fSU->GetCurValue(ev);
  97.     return (this->GetPromiseInfo(value) ? kODTrue : kODFalse);
  98. }
  99.  
  100. //------------------------------------------------------------------------------
  101. // PromiseResolver::SetSourcePart
  102. //------------------------------------------------------------------------------
  103.  
  104. void PromiseResolver::SetSourcePart(Environment* ev,
  105.                                      ODPart *sourcePart)
  106. {
  107.     ODValueRefCon*        oldRefCon;
  108.     ODValueRefCon*        newRefCon;
  109.     CMValue                value;
  110.     ProcessSerialNumber thePSN;
  111.     
  112.     value = fSU->GetCurValue(ev);
  113.  
  114.     OSErr result = GetCurrentProcess(&thePSN);
  115.     THROW_IF_ERROR(result);
  116.  
  117.     oldRefCon = (ODValueRefCon *) this->GetPromiseInfo(value);
  118.     if (oldRefCon == kODNULL)
  119.     {
  120.         this->IncrementPromiseCount();
  121.         newRefCon = (ODValueRefCon *) ODNewPtr(sizeof(ODValueRefCon),
  122.                                                 fSU->GetHeap(ev));
  123.         ODAcquireObject(ev,    sourcePart);
  124.         newRefCon->sourcePart = sourcePart;
  125.         newRefCon->sourcePSN = thePSN;
  126.         this->SetPromiseInfo(value, newRefCon);
  127.     }
  128.     else if (oldRefCon->sourcePart != sourcePart)
  129.     {
  130.         ODReleaseObject(ev, oldRefCon->sourcePart);
  131.         ODAcquireObject(ev,    sourcePart);
  132.         oldRefCon->sourcePart = sourcePart;
  133.         oldRefCon->sourcePSN = thePSN;
  134.     }
  135. }
  136.  
  137. //------------------------------------------------------------------------------
  138. // PromiseResolver::GetSourcePart
  139. //------------------------------------------------------------------------------
  140.  
  141. ODPart* PromiseResolver::GetSourcePart(Environment* ev)
  142. {    
  143.     CMValue value = fSU->GetCurValue(ev);
  144.  
  145.     ODValueRefCon* theRefCon = (ODValueRefCon *) this->GetPromiseInfo(value);
  146.     if (theRefCon == kODNULL) {
  147.         if (fResolvingPromiseInfo)
  148.             theRefCon = fResolvingPromiseInfo;
  149.         else
  150.             THROW(kODErrNotPromise);
  151.     }
  152.     return theRefCon->sourcePart;
  153. }
  154.  
  155. //------------------------------------------------------------------------------
  156. // PromiseResolver::ClearPromise
  157. //------------------------------------------------------------------------------
  158.  
  159. void PromiseResolver::ClearPromise(Environment* ev)
  160. {
  161.     CMValue value = fSU->GetCurValue(ev);
  162.     if (value != kODNULL) {
  163.         ODPtr theRefCon = this->GetPromiseInfo(value);
  164.         if (theRefCon) {
  165.             ODPart* sourcePart = ((ODValueRefCon*) theRefCon)->sourcePart;
  166.             if (sourcePart)
  167.                 ODReleaseObject(ev, sourcePart);
  168.             ODDisposePtr(theRefCon);
  169.             this->SetPromiseInfo(value, kODNULL);
  170.             this->DecrementPromiseCount();
  171.         }
  172.     }
  173. }
  174.  
  175. //------------------------------------------------------------------------------
  176. // PromiseResolver::ResolvePromise
  177. //------------------------------------------------------------------------------
  178.  
  179. void PromiseResolver::ResolvePromise(Environment* ev)
  180. {
  181.     CMValue value = fSU->GetCurValue(ev);
  182.     ODValueRefCon* theRefCon = (ODValueRefCon *) this->GetPromiseInfo(value);
  183.     if (theRefCon)
  184.     {
  185.         ODStorageUnitView*    destSUView = kODNULL;
  186.         ODVolatile(destSUView);
  187.         TRY
  188.             fResolvingPromiseInfo = theRefCon;
  189.  
  190.             // First we must remove the refCon to stop any recursion
  191.             this->SetPromiseInfo(value, kODNULL);
  192.             
  193.             // Create a view for this storage unit
  194.             destSUView = fSU->CreateView(ev);
  195.             
  196.             ODPart*    sourcePart = theRefCon->sourcePart;
  197.  
  198.             Boolean             sameProcess;
  199.             OSErr               result;
  200.             ProcessSerialNumber thePSN;
  201.             
  202.             result = GetCurrentProcess(&thePSN);
  203.             THROW_IF_ERROR(result);
  204.             result = SameProcess(&(theRefCon->sourcePSN), &thePSN, &sameProcess);
  205.             THROW_IF_ERROR(result);
  206.  
  207.             if (sourcePart && sameProcess) {
  208.                 sourcePart->FulfillPromise(ev, destSUView);
  209.             }
  210.             else {    
  211.                 ODDragAndDrop* dragAndDrop = fSU->GetSession(ev)->GetDragAndDrop(ev);
  212.                 if (dragAndDrop->GetDragReference(ev) != 0) {
  213.                     fSU->GetSession(ev)->GetDragAndDrop(ev)->GetPromiseFromDragManager(ev,
  214.                                                             theRefCon->sourcePart,
  215.                                                             destSUView);
  216.                 }
  217.                 else {
  218.                     WARN("Drag manager cannot fulfill promise.");
  219.                     THROW(kODErrCannotFulfillDragPromise);
  220.                 }
  221.             }
  222.     
  223.             destSUView->SetOffset(ev, 0);
  224.             delete destSUView;
  225.             ODReleaseObject(ev, sourcePart);
  226.             ODDisposePtr(theRefCon);
  227.             this->DecrementPromiseCount();
  228.         
  229.         CATCH_ALL
  230.         
  231.             if (destSUView)
  232.                 delete destSUView;
  233.  
  234.             fResolvingPromiseInfo = kODNULL;
  235.             if (ErrorCode() != kODErrUnfocusedStorageUnit)
  236.                 RERAISE;
  237.             
  238.         ENDTRY
  239.         
  240.         fResolvingPromiseInfo = kODNULL;
  241.     }
  242. }
  243.  
  244. //------------------------------------------------------------------------------
  245. // PromiseResolver::GetPromiseInfo
  246. //------------------------------------------------------------------------------
  247.  
  248. ODPtr PromiseResolver::GetPromiseInfo(CMValue curValue)
  249. {
  250.     return CMGetValueRefCon(curValue);
  251. }
  252.  
  253. //------------------------------------------------------------------------------
  254. // PromiseResolver::SetPromiseInfo
  255. //------------------------------------------------------------------------------
  256.  
  257. void PromiseResolver::SetPromiseInfo(CMValue curValue, ODPtr theInfo)
  258. {
  259.     CMSetValueRefCon(curValue, theInfo);
  260. }
  261.  
  262. //------------------------------------------------------------------------------
  263. // PromiseResolver::IncrementPromiseCount
  264. //------------------------------------------------------------------------------
  265.  
  266. void PromiseResolver::IncrementPromiseCount()
  267. {
  268.     fPromiseCount++;
  269. }
  270.  
  271. //------------------------------------------------------------------------------
  272. // PromiseResolver::DecrementPromiseCount
  273. //------------------------------------------------------------------------------
  274.  
  275. void PromiseResolver::DecrementPromiseCount()
  276. {
  277.     fPromiseCount--;
  278. }
  279.  
  280. //------------------------------------------------------------------------------
  281. // PromiseResolver::GetPromiseCount
  282. //------------------------------------------------------------------------------
  283.  
  284. ODULong PromiseResolver::GetPromiseCount()
  285. {
  286.     return fPromiseCount;
  287. }
  288.  
  289. //------------------------------------------------------------------------------
  290. // PromiseResolver::SettingPromise
  291. //------------------------------------------------------------------------------
  292.  
  293. void PromiseResolver::SettingPromise()
  294. {
  295.     fSettingPromise = kODTrue;
  296. }
  297.  
  298. //------------------------------------------------------------------------------
  299. // PromiseResolver::DoneSettingPromise
  300. //------------------------------------------------------------------------------
  301.  
  302. void PromiseResolver::DoneSettingPromise()
  303. {
  304.     fSettingPromise = kODFalse;
  305. }
  306.  
  307. //------------------------------------------------------------------------------
  308. // PromiseResolver::IsSettingPromise
  309. //------------------------------------------------------------------------------
  310.  
  311. ODBoolean PromiseResolver::IsSettingPromise()
  312. {
  313.     return fSettingPromise;
  314. }
  315.  
  316. //------------------------------------------------------------------------------
  317. // PromiseResolver::PromiseResolver
  318. //------------------------------------------------------------------------------
  319. PromiseResolver::PromiseResolver()
  320. {
  321.     fSU = kODNULL;
  322.     fPromiseCount = 0;
  323.     fSettingPromise = kODFalse;
  324.     fResolvingPromiseInfo = kODNULL;
  325. }
  326.  
  327. //------------------------------------------------------------------------------
  328. // PromiseResolver::~PromiseResolver
  329. //------------------------------------------------------------------------------
  330. PromiseResolver::~PromiseResolver()
  331. {
  332. }
  333.  
  334. //------------------------------------------------------------------------------
  335. // PromiseResolver::InitPromiseResolver
  336. //------------------------------------------------------------------------------
  337. void PromiseResolver::InitPromiseResolver(CMStorageUnit* su)
  338. {
  339.     fSU = su;
  340. }
  341.  
  342.  
  343.